/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package actions;

import SessionFactory.SessionFactoryUtil;
import beans.AnnouncementBean;
import client.AnnAndWarn;
import dto.Announcement;
import dto.AnnouncementHome;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
//import java.util.Calendar;
//import java.util.Date;
import java.util.Calendar;
import java.util.Date;
import java.util.Vector;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

/**
 *
 * @author A7med Kotb
 */
public class AnnouncementAction extends org.apache.struts.actions.DispatchAction {

    /* forward name="success" path="" */
    private static final String SUCCESS = "success";

    /**
     * This is the action called from the Struts framework.
     * @param mapping The ActionMapping used to select this instance.
     * @param form The optional ActionForm bean for this request.
     * @param request The HTTP Request we are processing.
     * @param response The HTTP Response we are processing.
     * @throws java.lang.Exception
     * @return
     */
    public ActionForward Announcements(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception 
    {
        Vector<Announcement> annvect=new Vector<Announcement>();
        AnnouncementHome annhome=new AnnouncementHome();
        SessionFactoryUtil.startNew();
        annvect=annhome.getAllAnnouncements();
        boolean b = SessionFactoryUtil.commitTrans();
        SessionFactoryUtil.finish();
        AnnouncementBean annbean=(AnnouncementBean)form;
        annbean.setVect(annvect);
        System.out.println("Vector Size =============================================="+annvect.size());
        annbean.setAnnBody("");
        return mapping.findForward(SUCCESS);
    }
    public ActionForward Add(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception 
    {
        AnnouncementBean annbean=(AnnouncementBean)form;
        Announcement ann=new Announcement();
        ann.setAnnBody(annbean.getAnnBody());
        System.out.println(annbean.getAnnBody());
        try
        {
            SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MMM-yy");
            DateFormat df=new SimpleDateFormat("dd-MMM-yy");
            Calendar cal=Calendar.getInstance();
            Date anndate=df.parse(dateFormat.format(cal.getTime()));
            ann.setAnnDuration(anndate);
            System.out.println(anndate);
        }
        catch(ParseException e)
        {
            e.printStackTrace();
        }
        AnnouncementHome annhome=new AnnouncementHome();
        SessionFactoryUtil.startNew();
        annhome.persist(ann);
         boolean b = SessionFactoryUtil.commitTrans();
        SessionFactoryUtil.finish();
        ActionForward forward=new ActionForward();
        
        
                //mapping.findForward(SUCCESS);      
        //=============Maha==========================
        //============test===========================
                AnnAndWarn announcement=new AnnAndWarn();
                Vector<Announcement> announcement1 = announcement.getannouncement();
                request.getSession(false).setAttribute("announcement",announcement1);
        //===========================================
//        return mapping.findForward(SUCCESS);\
                return forward=Announcements(mapping, form, request, response);
}
    
    public ActionForward Delete(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception 
    {
        int id=((AnnouncementBean) form).getAnnId();
        System.out.println("///////////////////////////////////////////////"+id);
        System.out.println("/////////////////////////////////////"+((AnnouncementBean) form).getAnnBody());
        System.out.println("/////////////////////////////////////"+((AnnouncementBean) form).getAnnDuration());
        Announcement ann=new Announcement();
//        ann.setAnnId(id);
////        ann.setAnnDuration(((AnnouncementBean) form).getAnnDuration());
//        ann.setAnnBody(((AnnouncementBean) form).getAnnBody());
        AnnouncementHome annhome=new AnnouncementHome();
        SessionFactoryUtil.startNew();
        ann=annhome.findById(id);
        annhome.delete(ann);
        boolean b = SessionFactoryUtil.commitTrans();
        SessionFactoryUtil.finish();
        ActionForward forward=new ActionForward();
        return forward=Announcements(mapping, form, request, response);
    }
    
}
